package tab

import (
	"fmt"
	"github.com/liushuochen/gotable"
	"github.com/liushuochen/gotable/cell"
	"github.com/liushuochen/gotable/table"
	"strings"
)

func Of(colums []string) (*Table, error) {

	table, err := gotable.Create("NAME")

	if err != nil {
		return nil, err
	}

	for _, v := range colums {
		name := strings.ToUpper(v)
		if name != "NAME" {
			if err = table.AddColumn(name); err != nil {
				return nil, err
			}
		}
		table.Align(name, cell.AlignLeft)
	}
	table.CloseBorder()
	return &Table{
		tab: table,
	}, nil
}

type Table struct {
	tab *table.Table
}

func (t *Table) AddRow(values map[string]string) {
	rows := make(map[string]string, 0)
	for k, v := range values {
		rows[strings.ToUpper(k)] = v
	}
	t.tab.AddRows([]map[string]string{rows})
}

func (t *Table) Print() {
	fmt.Println(t.tab)
}
